//
//  FilterView.m
//  AppleMarket
//
//  Created by yangy on 14/11/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "CoinRecordFilterView.h"

@interface CoinRecordFilterView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIView *blockView;
@property (nonatomic, strong) UIButton *resetButton;
@property (nonatomic, strong) UIButton *confirmButton;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIButton *nameButton;
@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UIButton *typeButton;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *coinArray;
@property (nonatomic, strong) NSArray *typeArray;
@property (nonatomic, strong) NSArray *dataList;

@property (nonatomic, assign) NSInteger curTags;
@end

@implementation CoinRecordFilterView

- (instancetype)initWithFrame:(CGRect)frame coinArr:(NSArray*) coinArray type:(NSArray*) typeArray;
{
    self = [super initWithFrame:frame];
    if(self){
        _coinArray = coinArray;
        _typeArray = typeArray;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        [self addSubview:self.blockView];
        [self.blockView addSubview:self.nameLabel];
        [self.blockView addSubview:self.nameButton];
        [self.blockView addSubview:self.typeLabel];
        [self.blockView addSubview:self.typeButton];
        [self.blockView addSubview:self.lineView];
        [self.blockView addSubview:self.resetButton];
        [self.blockView addSubview:self.confirmButton];
        [self addSubview:self.tableView];
        [self setViewsLayout];
    }
    return self;
}

- (void) setViewsLayout
{
    [_blockView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(self);
        make.height.mas_equalTo(213);
    }];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.blockView).offset(15);
        make.top.mas_equalTo(self.blockView).offset(41);
        make.width.mas_equalTo(61);
    }];
    
    [_nameButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel.mas_right);
        make.centerY.mas_equalTo(self.nameLabel.mas_centerY);
        make.right.mas_equalTo(self.blockView.mas_right).offset(-15);
        make.height.mas_equalTo(40);
    }];
    
    [_typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.blockView).offset(15);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(51);
        make.width.mas_equalTo(61);
    }];
    
    [_typeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.typeLabel.mas_right);
        make.centerY.mas_equalTo(self.typeLabel.mas_centerY);
        make.right.mas_equalTo(self.blockView.mas_right).offset(-15);
        make.height.mas_equalTo(40);
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.blockView);
        make.bottom.mas_equalTo(self.resetButton.mas_top);
        make.height.mas_equalTo(1);
    }];
    
    [_resetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.blockView);
        make.left.mas_equalTo(self.blockView);
        make.width.mas_equalTo(self.blockView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(52);
    }];
    [_confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.blockView);
        make.right.mas_equalTo(self.blockView);
        make.height.mas_equalTo(52);
        make.width.mas_equalTo(self.blockView.mas_width).multipliedBy(0.5);
    }];

}

- (void) showInView:(UIView*) view
{
    
    if([self superview]){
        [self removeFromSuperview];
    }else{
        [view addSubview:self];
    }
}

- (void) showPopView:(UIButton*) sender
{
    CGRect rect = sender.frame;
    
    self.dataList = sender.tag == 100? self.coinArray : self.typeArray;
    [self.tableView reloadData];
    
    if(CGRectGetMaxY(rect) == self.tableView.y && self.tableView.height != 0){
        self.tableView.height = 0;
    }else{
        self.curTags = sender.tag;
        CGFloat height = 41 * self.dataList.count;
        CGFloat maxHeight = self.bounds.size.height - CGRectGetMaxY(rect) - NAVIGATION_BAR_HEIGHT;
        height = height > maxHeight? maxHeight : height;
        self.tableView.frame = CGRectMake(rect.origin.x,CGRectGetMaxY(rect), rect.size.width, height);
    }
}

- (void) reset
{
    [self.nameButton setTitle:[self.coinArray firstObject] forState:UIControlStateNormal];
    [self.typeButton setTitle:[self.typeArray firstObject] forState:UIControlStateNormal];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.textLabel.font = kFont(15);
        cell.textLabel.textColor = kColorWithHex(0x0E304C);
        cell.backgroundColor = kColorWithHex(0xF7F9FC);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = self.dataList[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.curTags == 100){
        [self.nameButton setTitle:self.dataList[indexPath.row] forState:UIControlStateNormal];
    }else{
        [self.typeButton setTitle:self.dataList[indexPath.row] forState:UIControlStateNormal];
    }
    self.tableView.height = 0;
}

- (UIView *)blockView
{
    if(!_blockView){
        _blockView = [[UIView alloc] init];
        _blockView.backgroundColor = [UIColor whiteColor];
    }
    return _blockView;
}

- (UILabel *)nameLabel
{
    if(!_nameLabel){
        _nameLabel = [UILabel cm_labelWithText:@"币种" font:kFont(15) textColor:kColorWithHex(0x80878E) textAlign:NSTextAlignmentCenter];
    }
    return _nameLabel;
}

- (UIButton *)nameButton
{
    if(!_nameButton){
        _nameButton = [UIButton cm_ButtonWithTitle:[self.coinArray firstObject] font:kFontBold(15) titleColor:kColorWithHex(0x0E304C)];
        _nameButton.layer.borderColor = kColorWithHex(0xD6DBE4).CGColor;
        _nameButton.layer.borderWidth = 1;
        _nameButton.titleLabel.textAlignment = NSTextAlignmentLeft;
        _nameButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _nameButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        UIImageView *rightIcon = [[UIImageView alloc] init];
        rightIcon.image = [UIImage imageNamed:@"icon_down"];
        [_nameButton addSubview:rightIcon];
        [rightIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.nameButton.mas_right).offset(-15);
            make.centerY.mas_equalTo(self.nameButton);
        }];
        _nameButton.tag = 100;
        [[_nameButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self showPopView:x];
        }];
    }
    return _nameButton;
}

- (UILabel *)typeLabel
{
    if(!_typeLabel){
        _typeLabel = [UILabel cm_labelWithText:@"类型" font:kFont(15) textColor:kColorWithHex(0x80878E) textAlign:NSTextAlignmentCenter];
    }
    return _typeLabel;
}

- (UIButton *)typeButton
{
    if(!_typeButton){
        _typeButton = [UIButton cm_ButtonWithTitle:[self.typeArray firstObject] font:kFontBold(15) titleColor:kColorWithHex(0x0E304C)];
        _typeButton.layer.borderColor = kColorWithHex(0xD6DBE4).CGColor;
        _typeButton.layer.borderWidth = 1;
        _typeButton.titleLabel.textAlignment = NSTextAlignmentLeft;
        _typeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _typeButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        UIImageView *rightIcon = [[UIImageView alloc] init];
        rightIcon.image = [UIImage imageNamed:@"icon_down"];
        [_typeButton addSubview:rightIcon];
        _typeButton.tag = 101;
        
        [rightIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.typeButton.mas_right).offset(-15);
            make.centerY.mas_equalTo(self.typeButton);
        }];
        
        [[_typeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self showPopView:x];
        }];
    }
    return _typeButton;
}

- (UIView *)lineView
{
    if(!_lineView){
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kColorWithHex(0xEFEFEF);
    }
    return _lineView;
}

- (UIButton *)resetButton
{
    if(!_resetButton){
        _resetButton = [UIButton cm_ButtonWithTitle:@"重置" font:kFont(15) titleColor:kColorWithHex(0x4A647A)];
        [[_resetButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self reset];
        }];
    }
    return _resetButton;
}

- (UIButton *)confirmButton
{
    if(!_confirmButton){
        _confirmButton = [UIButton cm_ButtonWithTitle:@"确定" font:kFont(15) titleColor:[UIColor whiteColor] bgColor:kColorWithHex(0x3291EB)];
        [[_confirmButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            NSString *coinStr = self.nameButton.titleLabel.text;
            NSString *typeStr = self.typeButton.titleLabel.text;
            __block NSInteger coinIndex,typeIndex = 0;
            [self.coinArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if([obj isEqualToString:coinStr]){
                    coinIndex = idx;
                    *stop = YES;
                }
            }];
            [self.typeArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if([obj isEqualToString:typeStr]){
                    typeIndex = idx;
                    *stop = YES;
                }
            }];
            
            if(self.delegate && [self.delegate respondsToSelector:@selector(filterView:didSearchCoinIndex:typeIndex:)]){
                [self.delegate filterView:self didSearchCoinIndex:coinIndex typeIndex:typeIndex];
            }
            
            if(self.superview){
                [self removeFromSuperview];
            }
        }];
    }
    return _confirmButton;
}

- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setBounces:NO];
        [_tableView setRowHeight:41];
        [_tableView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.5]];
    }
    return _tableView;
}

@end
